﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson10
{
    public class Camera : GameComponent
    {
        protected Matrix _View;
        protected Matrix _Projection;

        public Matrix View
        {
            get
            {
                return _View;
            }
        }

        public Matrix Projection
        {
            get
            {
                return _Projection;
            }
        }

        private Vector3 _cameraPosition;

        public Vector3 CameraPosition
        {
            get
            {
                return _cameraPosition;
            }
        }

        private Vector3 _cameraDirection;

        public Vector3 CameraDirection
        {
            get
            {
                return _cameraDirection;
            }
        }

        private Vector3 _cameraUpVector;

        private float _speed = 1;
        private float _totalYaw = MathHelper.PiOver4 / 2;
        private float _currentYaw;
        private float _totalPitch = MathHelper.PiOver4 / 2;
        private float _currentPitch;

        public Camera(Game game, Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) : base(game)
        {
            game.Window.SizeChanged += WindowOnSizeChanged;
            _cameraPosition = cameraPosition;
            _cameraDirection = cameraTarget - cameraPosition;
            _cameraDirection.Normalize();
            _cameraUpVector = cameraUpVector;
            CreateLookAt();
        }

        private void WindowOnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            _Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float) (e.NewSize.Width / e.NewSize.Height), 1.0f, 3000f);
        }

        private void CreateLookAt()
        {
            _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirection, _cameraUpVector);
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            var keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Key.W))
            {
                var cameraDirection = _cameraDirection;
                cameraDirection.Y = 0;
                cameraDirection.Normalize();
                _cameraPosition += cameraDirection * _speed;
            }
            if (keyboardState.IsKeyDown(Key.S))
            {
                var cameraDirection = _cameraDirection;
                cameraDirection.Y = 0;
                cameraDirection.Normalize();
                _cameraPosition -= cameraDirection * _speed;
            }
            if (keyboardState.IsKeyDown(Key.A))
            {
                var cameraDirection = _cameraDirection;
                cameraDirection.Y = 0;
                cameraDirection.Normalize();
                _cameraPosition += Vector3.Cross(_cameraUpVector, cameraDirection) * _speed;
            }
            if (keyboardState.IsKeyDown(Key.D))
            {
                var cameraDirection = _cameraDirection;
                cameraDirection.Y = 0;
                cameraDirection.Normalize();
                _cameraPosition -= Vector3.Cross(_cameraUpVector, cameraDirection) * _speed;
            }

            if (keyboardState.IsKeyDown(Key.Left))
            {
                const float yaw = MathHelper.PiOver4 / 60;
                if (Math.Abs(_currentYaw + yaw) < _totalYaw)
                {
                    _cameraDirection = Vector3.Transform(_cameraDirection, Matrix.CreateFromAxisAngle(_cameraUpVector, yaw));
                    _currentYaw += yaw;
                }
            }
            if (keyboardState.IsKeyDown(Key.Right))
            {
                const float yaw = -MathHelper.PiOver4 / 60;
                if (Math.Abs(_currentYaw + yaw) < _totalYaw)
                {
                    _cameraDirection = Vector3.Transform(_cameraDirection, Matrix.CreateFromAxisAngle(_cameraUpVector, yaw));
                    _currentYaw += yaw;
                }
            }

            //if (keyboardState.IsKeyDown(Key.Q))
            //{
            //    _cameraUpVector = Vector3.Transform(_cameraUpVector, Matrix.CreateFromAxisAngle(_cameraDirection, MathHelper.PiOver4 / 60));
            //}
            //if (keyboardState.IsKeyDown(Key.E))
            //{
            //    _cameraUpVector = Vector3.Transform(_cameraUpVector, Matrix.CreateFromAxisAngle(_cameraDirection, -MathHelper.PiOver4 / 60));
            //}

            if (keyboardState.IsKeyDown(Key.Up))
            {
                var cameraLeftVector = Vector3.Cross(_cameraUpVector, _cameraDirection);
                const float pitch = -MathHelper.PiOver4 / 120;
                if (Math.Abs(_currentPitch + pitch) < _totalPitch)
                {
                    _cameraDirection = Vector3.Transform(_cameraDirection, Matrix.CreateFromAxisAngle(cameraLeftVector, pitch));
                    //_cameraUpVector = Vector3.Transform(_cameraUpVector, Matrix.CreateFromAxisAngle(cameraLeftVector, pitch));
                    _currentPitch += pitch;
                }
            }
            if (keyboardState.IsKeyDown(Key.Down))
            {
                var cameraLeftVector = Vector3.Cross(_cameraUpVector, _cameraDirection);
                const float pitch = MathHelper.PiOver4 / 120;
                if (Math.Abs(_currentPitch + pitch) < _totalPitch)
                {
                    _cameraDirection = Vector3.Transform(_cameraDirection, Matrix.CreateFromAxisAngle(cameraLeftVector, pitch));
                    //_cameraUpVector = Vector3.Transform(_cameraUpVector, Matrix.CreateFromAxisAngle(cameraLeftVector, pitch));
                    _currentPitch += pitch;
                }
            }

            CreateLookAt();
            base.Update(gameTime);
        }
    }
}